//
// @author				mitchell
// @description		020101.html页面的JS打包文件 注意引用顺序
// @lastmodified		$2010-7 - 15$
$(document).ready(function(){
	$('.map').click(function(event){
	  	event.preventDefault();
	  	var st = $(document).scrollTop();
		$("#search_map1").show();
		//$("html,body").animate({scrollTop: $(this).offset().top}, 900); 
		mapoption();
    });
	var map;
	var position_marker = false;
	function creat_map(latitude,longitude){
		//默认中心位置
		latlng = new google.maps.LatLng(39.9579201,116.3876719);
		var myOptions = {
			    zoom: 12,
			    center: latlng,
			    mapTypeId: 'roadmap',
			    mapTypeControl:false,
			    streetViewControl:false,
			    navigationControl:true,
			    navigationControlOptions: {
			        style: google.maps.NavigationControlStyle.ZOOM_PAN,
			        position: google.maps.ControlPosition.TOP_LEFT
			    },
			    scaleControl: true 
		}; 
		map = new google.maps.Map(document.getElementById("search_map1"), myOptions);
		var homeControlDiv = document.createElement('DIV');
		homeControlDiv.setAttribute("class", "map_storeinfo");
		var child_dom = document.getElementById("m_storeinfo");
		homeControlDiv.appendChild(child_dom);
		homeControlDiv.index = 1;
		map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
 		return map;
	}

	function mapoption(){
		creat_map();
		google.maps.event.addListener(map, 'click', function(event) {
			if(position_marker) position_marker.setVisible(false);
			
			placeMarker(event.latLng);
			$("#m_storeinfo").html('加载中......');
			$(".s_pic").hide();
			$(".s_load").show();
			google.maps.event.addListener(position_marker, 'drag', function() {
				//$("#latlnginfo").html(position_marker.getPosition().lat()+','+position_marker.getPosition().lng());
			});
				  
			google.maps.event.addListener(position_marker, 'dragend', function() {
				$("#m_storeinfo").html('加载中......');
				$(".s_load").show();
				$(".s_pic").hide();
				geocodePosition(position_marker.getPosition());
			});
		});
	}
	
	function geocodePosition(pos) {
		$(".s_load").hide();
		$("#m_storeinfo").html('');
		$('#notice_click_map').hide();
		$('#notice_drag_map').show();
	}
	
	function placeMarker(location) {
		map.setZoom(16);
		map.panTo(location);
		position_marker = new google.maps.Marker({
			position: location,
			draggable: true,
			map: map
		});
		$(".lat").val(position_marker.getPosition().lat());
		$(".lng").val(position_marker.getPosition().lng());
		geocodePosition(position_marker.getPosition());
		return position_marker;
	}	
});
